﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace System
{
    public static class ExpandoObjectExtensions
    {
        public static T Cast<T>(this ExpandoObject source)
        {
            IDictionary<string, object> dict = source;
            var ctor = typeof(T).GetConstructors().Single();
            var parameters = ctor.GetParameters();
            var parameterValues = parameters.Select(p => dict[p.Name]).ToArray();
            return (T)ctor.Invoke(parameterValues);
        }
    }
}
